package cn.shujuhai.cms.service.impl;

import cn.shujuhai.cms.domain.Group;
import cn.shujuhai.cms.domain.StructParam;
import cn.shujuhai.cms.domain.SysChannel;
import cn.shujuhai.cms.service.StructParamService;
import cn.shujuhai.cms.service.SysChannelService;
import cn.shujuhai.common.pluging.data.ResultCode;
import cn.shujuhai.common.pluging.data.ResultVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.shujuhai.cms.domain.Struct;
import cn.shujuhai.cms.service.StructService;
import cn.shujuhai.cms.mapper.StructMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 4g0
 * @description 针对表【struct】的数据库操作Service实现
 * @createDate 2025-08-05 15:06:59
 */
@Service
@RequiredArgsConstructor
public class StructServiceImpl extends ServiceImpl<StructMapper, Struct>
        implements StructService {
    private final StructParamService structParamService;
    private final SysChannelService sysChannelService;

    @Override
    public ResultVo<PageInfo<Struct>> getStructPageInfo(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Struct> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }
        List<Struct> structList = this.list(queryWrapper.lambda().orderByDesc(Struct::getStructId));  // 根据用户Id降序排序
        PageInfo<Struct> structPageInfo = new PageInfo<>(structList);
        return ResultVo.success(structPageInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo<?> add(Struct struct) {
        if (!this.save(struct)) {
            return ResultVo.failure(ResultCode.FAILED);
        }
        List<StructParam> structParamList = struct.getStructParamList();
        for (StructParam structParam : structParamList) {
            structParam.setStructId(struct.getStructId());
        }
        if (!this.structParamService.saveBatch(structParamList)) {
            return ResultVo.failure(ResultCode.FAILED);
        }
        return ResultVo.success();
    }

    @Override
    public ResultVo<?> getStructParamByStructId(Long structId) {
        List<StructParam> list = this.structParamService.list(new QueryWrapper<StructParam>().lambda().eq(StructParam::getStructId, structId));
        if (list.isEmpty()) {
            return ResultVo.success(Collections.emptyList());
        }
        return ResultVo.success(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo<?> edit(Struct struct) {
        List<SysChannel> list = this.sysChannelService.list(new QueryWrapper<SysChannel>().lambda().eq(SysChannel::getStructId, struct.getStructId()));
        if (!list.isEmpty()) {
            return ResultVo.failure(ResultCode.STRUCT_NO_EDIT);
        }
        if (!this.updateById(struct)) {
            return ResultVo.failure(ResultCode.FAILED);
        }
        this.structParamService.remove(new QueryWrapper<StructParam>().lambda().eq(StructParam::getStructId, struct.getStructId()));
        List<StructParam> structParamList = struct.getStructParamList();
        for (StructParam structParam : structParamList) {
            structParam.setStructId(struct.getStructId());
        }
        if (!this.structParamService.saveBatch(structParamList)) {
            return ResultVo.failure(ResultCode.FAILED);
        }
        return ResultVo.success();
    }

    @Override
    public ResultVo<?> removeId(Long structId) {
        List<SysChannel> list = this.sysChannelService.list(new QueryWrapper<SysChannel>().lambda().eq(SysChannel::getStructId, structId));
        if (!list.isEmpty()) {
            return ResultVo.failure(ResultCode.NO_REMOVE);
        }
        this.structParamService.remove(new QueryWrapper<StructParam>().lambda().eq(StructParam::getStructId, structId));
        if (!this.removeById(structId)) {
            ResultVo.failure(ResultCode.FAILED);
        }
        return ResultVo.success();
    }

    @Override
    public ResultVo<?> getStructList() {
        List<Struct> list = this.list();
        if (list.isEmpty()) {
            return ResultVo.success(Collections.emptyList());
        }
        return ResultVo.success(list);
    }
}




